package com.hy.${packageCode}.server.core.service;

import com.hy.${packageCode}.common.api.result.ApiPageResult;
import com.hy.${packageCode}.server.common.dto.${classCode}DTO;
import com.hy.${packageCode}.server.common.param.${classCode}Param;
import com.hy.${packageCode}.server.common.param.${classCode}QueryParam;
import com.hy.${packageCode}.server.common.param.${classCode}UpdateParam;
import com.hy.${packageCode}.server.common.vo.${classCode}ItemVO;
import com.hy.${packageCode}.server.core.dbaccess.${classCode}PO;
import com.hy.${packageCode}.server.core.dbaccess.${classCode}Repository;
import com.hy.${packageCode}.server.core.dbaccess.Q${classCode}PO;
import com.hy.${packageCode}.shared.exception.BusinessException;
import com.hy.${packageCode}.shared.utils.CommonUtils;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;
<#if containString>
import org.springframework.util.StringUtils;
</#if>

<#if containNotString>
import java.util.Objects;
</#if>

@Service
public class ${classCode}Service {

    private static final Q${classCode}PO q${classCode}PO = Q${classCode}PO.${objCode}PO;
    private static final OrderSpecifier<?> defaultOrder = q${classCode}PO.id.desc();
    @Autowired
    private ${classCode}Repository ${objCode}Repository;

    public ApiPageResult<${classCode}ItemVO> page(${classCode}QueryParam queryParam, Integer pageNum, Integer pageSize) {
        return ApiPageResult.with(${objCode}Repository.findAll(buildCondition(queryParam),
                        QPageRequest.of(pageNum - 1, pageSize, defaultOrder))
                .map(this::convertItemVO));

    }

    private ${classCode}ItemVO convertItemVO(${classCode}PO ${objCode}PO) {
        return ${classCode}ItemVO.builder()
                .id(${objCode}PO.getId())
<#list parameters as parameter>
                .${parameter.fieldName}(${objCode}PO.get${parameter.fieldName?cap_first}())
</#list>
                .build();
    }


    public ${classCode}DTO detail(Long id) {
        ${classCode}PO ${objCode}PO = ${objCode}Repository.findById(id)
                .orElseThrow(() -> new BusinessException("查询的数据不存在！"));
        return ${classCode}DTO.builder()
                .id(${objCode}PO.getId())
<#list parameters as parameter>
                .${parameter.fieldName}(${objCode}PO.get${parameter.fieldName?cap_first}())
</#list>
                .build();
    }

    public void add(${classCode}Param param) {
        ${classCode}PO ${objCode}PO = CommonUtils.copyProperties(param, ${classCode}PO.class);
        ${objCode}Repository.save(${objCode}PO);
    }

    public void update(${classCode}UpdateParam param) {
        ${classCode}PO ${objCode}PO = ${objCode}Repository.findById(param.getId())
                .orElseThrow(() -> new BusinessException("修改的数据不存在！"));
        CommonUtils.copyProperties(param, ${objCode}PO);
        ${objCode}Repository.save(${objCode}PO);
    }

    public void delete(Long id) {
        ${objCode}Repository.deleteById(id);
    }

    private BooleanExpression buildCondition(${classCode}QueryParam queryParam) {
        BooleanExpression condition = q${classCode}PO.isNotNull();
<#list parameters as parameter>
    <#if parameter.fieldType == "String">
    if (StringUtils.hasLength(queryParam.get${parameter.fieldName?cap_first}())) {
        condition = condition.and(q${classCode}PO.${parameter.fieldName}.eq(queryParam.get${parameter.fieldName?cap_first}()));
    }
    </#if>
    <#if parameter.fieldType != "String">
    if (Objects.nonNull(queryParam.get${parameter.fieldName?cap_first}())) {
        condition = condition.and(q${classCode}PO.${parameter.fieldName}.eq(queryParam.get${parameter.fieldName?cap_first}()));
    }
    </#if>
</#list>
        return condition;
    }
}
